package com.example.demo.controller;

import com.example.demo.pojo.Accounts.UserFundAccounts;
import com.example.demo.pojo.R;
import com.example.demo.service.UserAccountService;
import com.example.demo.utils.TokenUtil.JwtUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/userAccounts")
public class UserAccountController {

    private static final Logger logger = LoggerFactory.getLogger(UserAccountController.class);

    @Resource
    private UserAccountService userAccountService;

    /**
     * 查询当前用户银行账户信息接口。
     *
     * @param id    比赛id
     * @param token 用户登录时获取的Token
     * @return 返回用户银行账户信息
     */
    @GetMapping("/getUserAccount/{id}")
    public R<List<UserFundAccounts>> getUserAccount(@PathVariable Integer id,
                                                    @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return userAccountService.getUserAccount(id, token);
        } catch (Exception e) {
            logger.error("获取银行账户信息失败", e);
            return R.failed("获取银行账户信息失败");
        }
    }

    /**
     * 设置银行账户初始金额。
     *
     * @param accounts 金额数值
     * @param id       比赛id
     * @param token    用户登录时获取的Token
     * @return 设置结果
     */
    @PutMapping("/setUserFundAccount/{id}")
    public R<T> setUserFundAccount(@RequestParam BigDecimal accounts,
                                   @PathVariable int id,
                                   @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return userAccountService.setUserFundAccount(token, accounts, id);
        } catch (Exception e) {
            logger.error("设置银行账户初始金额失败", e);
            return R.failed("设置失败");
        }
    }

    /**
     * 转账操作接口。
     *
     * @param id            比赛id
     * @param transferFunds 转账金额
     * @param token         用户登录时获取的Token
     * @return 转账结果
     */
    @PutMapping("/transferOfFunds/{id}")
    public R<T> userTransferOfFunds(@PathVariable Integer id,
                                    @RequestParam BigDecimal transferFunds,
                                    @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return userAccountService.userTransferOfFunds(token, id, transferFunds);
        } catch (Exception e) {
            logger.error("转账操作失败", e);
            return R.failed("转账失败");
        }
    }

}
